<?php

$LANG=getParameter("language");
if (getParameter("operation")=="list")
{
    printArticles();


}elseif (getParameter("operation")=="delete")
{
    $articleId=getParameter("articleId");
    for($i = 0; $i < count($_SESSION['panier']['unitId']); $i++)
    {

        if ($articleId==$_SESSION['panier']['unitId'][$i])
        {
            $_SESSION['panier']['quantity'][$i]--;
            if ($_SESSION['panier']['quantity'][$i]<=0) supprimerArticle($articleId);
      //  echo $_SESSION['panier']['quantity'][$i];
        }
    }
  printArticles();

}elseif (getParameter("operation")=="add")
{
	$barCode=getParameter("barCode");
	//$barCode=substr($barCode, 0,strlen($barCode)-1);


    $barCode=fixBarCode($barCode);

    if ($app_shop_hasUnit=="yes"){
	$unitId=getObjByKey("shop_unit","id","portalId",$portalId,"barCode",$barCode);
    }else{

        $unitId=getObjByKey("shop_articles","id","portalId",$portalId,"barCode",$barCode);
    }
    //echo $unitId.$app_shop_hasUnit;
if ($unitId=="")
{
	ShowError("Produit Inconnu.");
}
else{
	$quantity=1;
	$size=1;
	if ($unitId) ajouterArticle($unitId, $quantity, $size);

	printArticles();
	}}elseif (getParameter("operation")=="getClients")
{
	
	$sSql ="SELECT * FROM `shop_clients` ";
	$rs=$GLOBALS["conn"]->Execute($sSql);
	// Portal_log($sSql);
	$json_names = array();
	while ( !$rs->EOF)  {
		$json_names[] = array('id' =>  $rs->fields["id"],'label' =>      $rs->fields["firstname"],'value' =>      $rs->fields["lastname"]);
		$rs->MoveNext();
	}
	



}


function printArticles()
{
    if (isset($_SESSION['panier'])){
    global $portalId,$LANG,$app_shop_hasUnit,$app_shop_currency;
    ?>
<span><div class="top">
</div>
<table width="100%" class="caissetable">
    <thead>
    <tr class="toptable">

        <td width="15px" class="col1"></td>
        <td class="col2">Article</td>
        <TD class="col3">Forme</td>
        <td class="col4">Quantité</td>

        <td class="col5">Prix</td>
        <td class="col6">Sous Total</td>
        <td class="col7">Stock</td>
        <td style="width: 72px;"  class="col8">Supprimer</td>
    </tr>
    </thead>
    <tbody>
    <?php
    $total=0;
    for($i = 0; $i < count($_SESSION['panier']['unitId']); $i++)
    {

        $id=$_SESSION['panier']['unitId'][$i];
        $sSql="SELECT
	shop_articles.price,
	shop_articles.lib_$LANG as lib,
	shop_articles.id,
	shop_articles.reference,
	shop_articles.description_$LANG as description,
	shop_articles.availability

	FROM
	shop_articles

	WHERE
	shop_articles.id = ".$id." AND
	shop_articles.portalId = ".$portalId;
//echo $sSql;
        Portal_log($sSql);
        $rs=$GLOBALS["conn"]->Execute($sSql);
        if ($rs && $rs->RecordCount()>0) {
            $price=$rs->fields["price"];
            $articleId=$rs->fields["id"];
            $lib=$rs->fields["lib"];
            $availability=$rs->fields["availability"];
            $description=$rs->fields["description"];
            $reference=$rs->fields["reference"];
            $size=0;//$rs->fields["size"];
            $picture="";//$rs->fields["picture"];
            $quantity=$_SESSION['panier']['quantity'][$i];
            $_SESSION['panier']['size'][$i]=$size;
            $class="odded";
            if ($i%2) $class="even";
            ?>
            <tr class="line <?php echo $class; ?>" id="article_<?php echo $id; ?>">
                <td class="col1"></td>
                <td class="col2">
                    <?php echo $lib; ?>

                    <!--	<div  id="image_<?php echo $id; ?>" class="image">
				<img src="<?php echo getFilesPath("gallery",false)."/".$picture; ?>" width="120px" height="120px" />

				</div>-->
                </td>
                <td class="col3">
                    <?php echo $description; ?>
                </td>
                <?php if ($app_shop_hasUnit=="yes") { ?>
                <td  class="col2"><?php echo $size; ?></td>
                <?php } ?>

                <td  class="col4">
                    <input type="hidden" name="unitId_<?php echo $i; ?>" value="<?php echo $id; ?>">
                    <?php echo $quantity; ?>


                </td>


                <td class="col5"><?php echo $price; ?> <?php echo $app_shop_currency; ?></td>

                <td class="col6"><?php echo $price*$quantity; ?> <?php echo $app_shop_currency; ?></td>
                <td class="col7"><?php echo $availability-$quantity; ?></td>


                <td class="col8">
                    <a onclick="deleteproduct(<?php echo $id; ?>)"  name="delete"><img src="/portalfiles/omnipharma/image/delete.png" alt="delete" /></a>
                </td>
            </tr>
            <?php

            $total+=$price*$quantity;


        }}




    ?>
    </tbody>
</table>
<div class="bottom">
</div>
	<div class="total">
        <div>Total : <input type="text" value="<?php echo $total; ?> <?php echo $app_shop_currency; ?>"> </div>
        </span>
	<?php

	}}?>